//MIT License
//
//Copyright(c) 2020 Goodman Tao
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files(the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions :
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.


#include "GlobalVars.h"


typedef struct
{
	Mutex* pMtx;
	ConditionVariable* pCondVar;
	int count;
	OnceFlag onceFlags[10000];
} Fld;

static void Clear(void* pParams)
{
	GlobalVars* pThis = pParams;

	Fld* pFld = pThis->Fld;
	pFld->pMtx->Destroy(pFld->pMtx);
	pFld->pCondVar->Destroy(pFld->pCondVar);
}

static void InitMutex(void* _pThis, va_list* pvlArgs)
{
	GlobalVars* pThis = _pThis;

	MutexType type = va_arg(*pvlArgs, MutexType);
	ConcurrenceReturnValue* pRetVal = va_arg(*pvlArgs, void*);

	Fld* pFld = pThis->Fld;

	pFld->pMtx = __NEW(Mutex);
	pFld->pMtx->Call(pFld->pMtx, "Init", type, pRetVal);
}

static void GetMutex(void* _pThis, va_list* pvlArgs)
{
	GlobalVars* pThis = _pThis;

	Mutex** pRetMtx = va_arg(*pvlArgs, Mutex**);

	Fld* pFld = pThis->Fld;

	*pRetMtx = pFld->pMtx;
}

static void InitCondVar(void* _pThis, va_list* pvlArgs)
{
	GlobalVars* pThis = _pThis;

	ConcurrenceReturnValue* pRetVal = va_arg(*pvlArgs, void*);

	Fld* pFld = pThis->Fld;

	pFld->pCondVar = __NEW(ConditionVariable);
	pFld->pCondVar->Call(pFld->pCondVar, "Init", pRetVal);
}

static void GetCondVar(void* _pThis, va_list* pvlArgs)
{
	GlobalVars* pThis = _pThis;

	ConditionVariable** pRetCondVar = va_arg(*pvlArgs, void**);

	Fld* pFld = pThis->Fld;

	*pRetCondVar = pFld->pCondVar;
}

static void GetCount(void* _pThis, va_list* pvlArgs)
{
	GlobalVars* pThis = _pThis;

	int* pRet = va_arg(*pvlArgs, int*);

	Fld* pFld = pThis->Fld;

	*pRet = pFld->count;
}

static void SetCount(void* _pThis, va_list* pvlArgs)
{
	GlobalVars* pThis = _pThis;

	int val = va_arg(*pvlArgs, int);

	Fld* pFld = pThis->Fld;

	pFld->count = val;
}

static void PP(void* _pThis, va_list* pvlArgs)
{
	GlobalVars* pThis = _pThis;

	Fld* pFld = pThis->Fld;
	pFld->count += 1;
}

static void MM(void* _pThis, va_list* pvlArgs)
{
	GlobalVars* pThis = _pThis;

	Fld* pFld = pThis->Fld;
	pFld->count -= 1;
}

static void _OnceFlag(void* _pThis, va_list* pvlArgs)
{
	GlobalVars* pThis = _pThis;

	OnceFlag** pRet = va_arg(*pvlArgs, void**);

	Fld* pFld = pThis->Fld;
	*pRet = pFld->onceFlags;
}

__CONSTRUCTOR(GlobalVars)
{
	return __Inst(__TYPE(GlobalVars), sizeof(Fld), Clear, 9, 0,
		__METHOD(InitMutex),
		__METHOD(GetMutex),
		__METHOD(InitCondVar),
		__METHOD(GetCondVar),
		__METHOD(GetCount),
		__METHOD(SetCount),
		"++", PP,
		"--", MM,
		"OnceFlag", _OnceFlag);
}

/****** static part **********************************************************************/
static ThreadLocal int threadLocalVar;
static int GetThreadLocalVar()
{
	return threadLocalVar;
}

static void SetThreadLocalVar(int a)
{
	threadLocalVar = a;
}

__STATICASSIGN(GlobalVars, GetThreadLocalVar, SetThreadLocalVar);
